package netty.decoder.delimiterbasedframedecoder.client;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class EchoClientHandler extends ChannelHandlerAdapter {

	private int counter = 0;
	
	private final String msg = "Hi,likaiqiang,welcome to netty$_";
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		ctx.close();
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		for(int i=0;i<10;i++){
			ByteBuf send = Unpooled.copiedBuffer(msg.getBytes());
			ctx.writeAndFlush(send);
		}
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		// TODO Auto-generated method stub
		String body = (String) msg;
		System.out.println("EchoClient receive " + ++counter + "msg: " + body);
	}
	
}
